package com.caiyi.financial.nirvana.sms.controller;

import com.alibaba.fastjson.JSONObject;
import com.caiyi.financial.nirvana.sms.enums.ReturnCodeEnum;
import com.caiyi.financial.nirvana.sms.querybean.SendParamsQueryBean;
import com.caiyi.financial.nirvana.sms.service.WebSendService;
import com.caiyi.financial.nirvana.sms.util.CommUtil;
import com.youyu.common.api.Result;
import com.youyu.common.enums.BaseResultCode;
import com.youyu.common.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;

@Api(description = "WEB页面短信发送")
@RestController
@CrossOrigin
@RequestMapping("sms/admin/webSend")
public class WebSendController {

    private static Logger logger = LoggerFactory.getLogger(WebSendController.class);

    @Autowired
    private WebSendService webSendService;

    @ApiOperation(value = "WEB页面短信发送", notes = "WEB页面短信发送")
    @ApiImplicitParam(name = "sendMessage", value = "短信发送", required = true, dataType = "SendParamsQueryBean")
    @PostMapping("/sendMessage")
    public Result sendMessage(@RequestBody SendParamsQueryBean sendParamsQueryBean) {
        JSONObject jsonObject = webSendService.sendMessage(sendParamsQueryBean);
        String code = jsonObject.getString("code");
        if (ReturnCodeEnum.SUCCESS.getCode().equals(code)) {
            return Result.ok(jsonObject.getString("desc"));
        }
        return Result.fail(jsonObject.getString("code"), jsonObject.getString("desc"));
    }

    @ApiOperation(value = "电话号码导入", notes = "电话号码导入")
    @ApiImplicitParam(name = "importPhone", value = "导入号码", required = true)
    @PostMapping("/importPhone")
    public Result importPhone(@RequestParam("file") MultipartFile file) throws Exception {
//        Set<String> phones = CommUtil.readLines(file.getInputStream());
//        return Result.ok(webSendService.importPhone(phones));
        throw new BizException(ReturnCodeEnum.FAILED);
    }


    @ApiOperation(value = "电话号码导入模板下载", notes = "电话号码导入模板下载")
    @ApiImplicitParam(name = "fileName", value = "文件名称", required = true, dataType = "String")
    @RequestMapping(value = "/downloadTemplate", method = RequestMethod.GET)
    public void downloadTemplate(HttpServletResponse response, @RequestParam("fileName") String fileName) {
        //设置响应头和客户端保存文件名
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
        InputStream inputStream = null;
        OutputStream os = null;
        try {
            inputStream = this.getClass().getResourceAsStream("/template/phoneTemplate.csv");
            //激活下载操作
            os = response.getOutputStream();
            //循环写入输出流
            byte[] b = new byte[2048];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
                logger.info("downloadTemplate INFO {}", new String(b));
            }
            os.flush();
        } catch (Exception e) {
            logger.error("downloadTemplate error {}", e);
        } finally {
            try {
                os.close();
                inputStream.close();
            } catch (Exception e) {

            }
        }
    }

}
